package com.yunc.base.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author taocongcong
 * @create 2018-06-08 15:59
 */
public class BaseUtil {

    public static final String NUMBER_CONTENT = "0123456789";

    /**
     * 获取文件的后缀
     */
    public static String acceptFileType(String fileName){
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        return suffix;
    }

    /**
     * 获取随机数
     */
    public static String acceptRandom(String content, int num){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num;i++){
            int id=(int)((Math.random())*(content.length()-1));
            sb.append(content.substring(id,id+1));
        }
        return sb.toString();
    }

    /**
     * 是否包含字符串
     *
     * @param str 验证字符串
     * @param strs 字符串组
     * @return 包含返回true
     */
    public static boolean inStringIgnoreCase(String str, String... strs) {
        if (str != null && strs != null) {
            for (String s : strs) {
                if (str.equalsIgnoreCase(trim(s))) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 去空格
     */
    public static String trim(String str) {
        return (str == null ? "" : str.trim());
    }

    public static String getFileNameByDate(String fileName){
        StringBuilder sb = new StringBuilder();
        sb.append(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN))
                .append(acceptRandom(BaseUtil.NUMBER_CONTENT, 6))
                .append(fileName.substring(fileName.lastIndexOf(".")));
        return sb.toString();
    }

    /**
     * 获取object的值
     *
     * @author fengshuonan
     * @Date 2019-04-04 17:07
     */
    public static Long getLong(Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof BigDecimal) {
            return ((BigDecimal) object).longValue();
        }
        if (object instanceof BigInteger) {
            return ((BigInteger) object).longValue();
        }
        if (object instanceof Long) {
            return ((Long) object);
        }
        return null;
    }
}
